<?php
/**
 * User: zhengze
 * Date: 2020/4/21
 * Time: 15:48
 */

namespace app\tool\controller;

use app\common\backup\BaseBackup;
use app\common\controller\BaseController;
use Helper\DateHelper;


class Backup extends BaseController
{

    /**
     * 初始化函数
     * @return boolean
     */
    protected function initialize()
    {
        parent::initialize();
        return true;
    }


    public function backup()
    {
//        docker exec -it php php /www/vue-think/php-api/public/index.php tool/backup/backup
        //删除7天前的备份
        $path = env('backup.basepath') . date('Y-m-d',DateHelper::daysAgo(7)) . '/';
        BaseBackup::delDir($path);
        //备份数据库
        $path = env('backup.basepath') . date('Y-m-d') . '/';
        $config = array(
            'path' => $path,//数据库备份路径
            'part' => 20971520,//数据库备份卷大小
            'compress' => 1,//数据库备份文件是否启用压缩 0不压缩 1 压缩
            'level' => 9 //数据库备份文件压缩级别 1普通 4 一般 9最高
        );
        $Backup = new BaseBackup($config);
        $tables = $Backup->dataList();//获取数据库所有表的信息
        foreach ($tables as $k => $v) {
            $Backup->setTimeout(0)->backup($v['name'], 0);//循环所有表备份表和数据
        }
        $file = $Backup->getFile();//获取所备份文件的文件名
        return $this->json_responce();
    }

}
